package com.myblog.demo.common;

import java.util.HashMap;
import java.util.Map;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 实现 ArticleInfoVO 与 Map 之间的相互转换
 */
import com.myblog.demo.entity.vo.ArticleInfoVO;

public class ArticleInfoVOConverterUtils {

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // 将ArticleInfoVO转换为Map
    public static Map<String, Object> convertToMap(ArticleInfoVO articleInfoVO) {
        Map<String, Object> map = new HashMap<>();

        // 将整字段转换为字符串
        map.put("id", String.valueOf(articleInfoVO.getId()));
        map.put("title", articleInfoVO.getTitle());
        map.put("content", articleInfoVO.getContent());
        map.put("createtime", dateTimeFormatter.format(articleInfoVO.getCreatetime()));
        map.put("updatetime", dateTimeFormatter.format(articleInfoVO.getUpdatetime()));
        map.put("uid", String.valueOf(articleInfoVO.getUid()));
        map.put("rcount", String.valueOf(articleInfoVO.getRcount()));
        map.put("state", String.valueOf(articleInfoVO.getState()));
        map.put("username", articleInfoVO.getUsername());
        map.put("nickname", articleInfoVO.getNickname());
        map.put("gitee", articleInfoVO.getGitee());

        return map;
    }

    // 将Map转换为ArticleInfoVO
    public static ArticleInfoVO convertToArticleInfoVO(Map<Object, Object> map) {
        ArticleInfoVO articleInfoVO = new ArticleInfoVO();

        // 从Map中获取值，并进行类型转换
        articleInfoVO.setId(Integer.parseInt((String) map.get("id")));
        articleInfoVO.setTitle((String) map.get("title"));
        articleInfoVO.setContent((String) map.get("content"));
        articleInfoVO.setCreatetime(LocalDateTime.parse((String) map.get("createtime"), dateTimeFormatter));
        articleInfoVO.setUpdatetime(LocalDateTime.parse((String) map.get("updatetime"), dateTimeFormatter));
        articleInfoVO.setUid(Integer.parseInt((String) map.get("uid")));
        articleInfoVO.setRcount(Integer.parseInt((String) map.get("rcount")));
        articleInfoVO.setState(Integer.parseInt((String) map.get("state")));
        articleInfoVO.setUsername((String) map.get("username"));
        articleInfoVO.setNickname((String) map.get("nickname"));
        articleInfoVO.setGitee((String) map.get("gitee"));

        return articleInfoVO;
    }
}
